var CouponCardTable = {
    id: "couponCardTable",//表格id
    seItem: null,		//选中的条目
    me : ""
};


/**
 * 初始化整个表格
 */
CouponCardTable.init = function(){
    var defaultColunms = this.initColumn();
    var bstable = new BSTable(CouponCardTable.id,Feng.ctxPath+"/couponCard/allParam",defaultColunms);
    bstable.setData({});
    this.me = bstable.init();
};

/**
 * 初始化表格的列
 */
CouponCardTable.initColumn = function(){
    var columns = [
        {title: '序号',align: 'center',valign: 'middle',
            formatter: function indexFormatter(value, row, index) {//在表格前加上序号
                return index+1;
            }},
        {title: '优惠券名称',field: 'couponName',align: 'center',valign: 'middle'},
        {title: '触发条件',field: 'useCondition',align: 'center',valign: 'middle',
            formatter:function(value,row,index){
                if(value == "0"){
                    return "无";
                }else if(value == "1"){
                    return "注册送";
                }else if (value =='2'){
                    return "资料完善送";
                } else if (value == '3') {
                    return "主动领取";
                } else if (value == '4') {
                    return "新用户专享";
                } else if (value == '5') {
                    return "老用户专享";
                } else {
                    return "其他";
                }
            }},
        {title: '使用课程类型',field: 'useTypeId',align: 'center',valign: 'middle',
            formatter:function(value,row,index){
                if(value == "0"){
                    return "全部";
                }else if(value == "1"){
                    return "课外辅导";
                }else if (value =='2'){
                    return "研学旅游";
                }else if (value =='3'){
                    return "冬夏令营";
                }else if (value =='4'){
                    return "线上辅导";
                }
            }},
        {title: '使用条件/(元)',field: 'conditionalAmount',align: 'center',valign: 'middle',
            formatter:function (value,row,index) {
                return numeral(value).format('0,0.00');
            }},
        {title: '优惠金额/(元)',field: 'discountAmount',align: 'center',valign: 'middle',
            formatter:function (value,row,index) {
                return numeral(value).format('0,0.00');
            }},
        {title: '使用期限', field: 'usefulTime', align: 'center', valign: 'middle'},
        {title: '优惠券数量/(张)',field: 'couponSum',align: 'center',valign: 'middle'},
        {title: '已使用/(张)',field: 'couponUserSum',align: 'center',valign: 'middle'},
        // {title: '创建者',field: 'createBy',align: 'center',valign: 'middle'},
        // {title: '创建时间',field: 'createDate',align: 'center',valign: 'middle'},
        {title: '状态',field: 'couponState',align: 'center',valign: 'middle',
            formatter:function (value,row,index) {
                if(row.isDelete == "1"){
                    return "<span class='label label-danger'>已停用</span>";
                } else if(value == "1"){
                    return "<span class='label label-success'>可以领用</span>";
                }else if(value == "2"){
                    return  "<span class='label label-warning'>已结束</span>";
                }
            }},
        {title: '操作',field: '#',align: 'center',
            formatter:function(value,row,index){
                var d = '<button class="btn btn-primary btn-xs" onclick="viewItems(\''+ row.id + '\')" title="查看使用详情"><i class="glyphicon glyphicon-align-justify"></i></button> &nbsp;&nbsp;';
                var e = '<button class="btn btn-primary btn-xs" onclick="editBtn(\''+ row.id + '\')" title="编辑"><i class="glyphicon glyphicon-pencil"></i></button> &nbsp;&nbsp;';
                var f = '<button class="btn btn-danger btn-xs" onclick="deleteBtn(\''+ row.id+'\',\''+ row.couponName+'\')" title="删除"><i class="glyphicon glyphicon-trash"></i></button>';
                return d+e+f;
            }
        }];
    return columns;
};

/**
 * 点击搜索按钮
 * @returns
 */
function clickSearch(){
    var queryData = {};
    var val_search_couponCardNo = $("#txt_search_couponCardNo").val();

    if (val_search_couponCardNo != "" || val_search_couponCardNo != null){
        queryData['couponCardNo'] =  val_search_couponCardNo;
    }

    CouponCardTable.me.destroy(queryData);
    // CouponCardTable.me.refresh({query: queryData});
}

/**
 * 点击添加按钮时，跳出添加用户对话框
 * @returns
 */
function clickAddBtn(){
    var index = layer.open({
        type: 1,
        title: '添加优惠券',
        skin: 'layui-layer-lan', //加上边框
        area: [CouponCardInfoDlg.width, "auto"],//宽高
        content: CouponCardInfoDlg.cache,
        success: function(layero, index){
            //层弹出成功后回调方法
            CouponCardInfoDlg.renderAdd();

        },
        maxmin: true,
        scrollbar: false,
        btn:['保存','关闭'],
        btnAlign: 'r',  //默认值
        yes:function (index,layero) {
            CouponCardInfoDlg.addSubmit(index);
            //layer.close(index); //如果设定了yes回调，需进行手工关闭
        },btn2:function (index) {
            layer.close(index);
        },
        cancel:function (index, layero) {
            //右上角关闭按钮触发的回调
            layer.close(index);
        }
    });
    layer.full(index);
    CouponCardInfoDlg.currentLayerIndex = index;
}

/**
 * 点击编辑按钮时，跳出用户详情对话框
 * @returns
 */
function editBtn(uid){
    var index = layer.open({
        type: 1,
        title: '编辑优惠券',
        skin: 'layui-layer-lan',
        area: [CouponCardInfoDlg.width,'auto'],
        content: CouponCardInfoDlg.cache,
        success: function(layero, index){
            //层弹出成功后回调方法
            CouponCardInfoDlg.renderAdd();
            CouponCardInfoDlg.couponCardId = uid;
            CouponCardInfoDlg.load(uid);
        },
        maxmin:true,
        scrollbar: false,
        btn:['保存','关闭'],
        btnAlign: 'r',  //默认值
        yes:function (index,layero) {
            CouponCardInfoDlg.editSubmit();
            layer.close(index); //如果设定了yes回调，需进行手工关闭
        },btn2:function (index) {
            layer.close(index);
        },
        cancel:function (index, layero) {
            //右上角关闭按钮触发的回调
            layer.close(index);
        }
    });
    layer.full(index);
    CouponCardInfoDlg.currentLayerIndex = index;
}


/**
 * 删除某个用户
 * @param uid 用户的id
 * @returns
 */
function deleteBtn(uid,deleteName){
    layer.confirm('是否删除优惠券:'+deleteName, {icon: 10, title:'是否删除优惠券?'}, function(index){
        var ajax = new $ax(Feng.ctxPath + "/couponCard/deleteById", function(data){
            if (data.success){
                Feng.success("删除成功!");
                CouponCardTable.me.refresh();
                layer.close(index);
            }else if(data.code === 2){
                Feng.fail(data.msg);
            }else{
                Feng.fail("删除失败!");
            }
        },function(data){
            Feng.error("删除失败!");
        });
        ajax.data={id:uid};
        ajax.start();
    });
}

/**
 * 查看优惠券使用详情
 * @returns
 */
function viewItems(dataId){
    var index = layer.open({
        type: 1,
        title: '优惠券使用详情列表',
        skin: 'layui-layer-lan', //加上边框
        area: [CouponCardItemsTable.width, CouponCardItemsTable.height],//宽高
        content: CouponCardItemsTable.cache,
        success: function(layero, index){
            //初始化优惠券管理表格
            CouponCardItemsTable.init(dataId);
        },
        maxmin: true,
        scrollbar: false,
        cancel:function (index, layero) {
            //右上角关闭按钮触发的回调
            layer.close(index);
        }
    });
    CouponCardItemsTable.currentLayerIndex = index;
}

$(function(){
    //初始化优惠券管理表格
    CouponCardTable.init();

    CouponCardInfoDlg.init();

    ReceiveUserBatchInfoDlg.init();

    CouponCardItemsTable.initView();
});
